package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.mappers.SysAdminMenuRoleMapper;
import cn.huiyunche.base.service.mappers.SysAdminRoleMapper;
import cn.huiyunche.base.service.mappers.SysAdminRoleUserMapper;
import cn.huiyunche.base.service.model.*;
import cn.huiyunche.base.service.vo.Result;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色
 *
 * @author hdy [Tuffy]
 */
@Service
public class RoleServiceImpl {

    @Autowired
    private SysAdminRoleMapper sysAdminRoleMapper = null;

    @Autowired
    private SysAdminRoleUserMapper sysAdminRoleUserMapper = null;

    @Autowired
    private SysAdminMenuRoleMapper sysAdminMenuRoleMapper = null;

    /**
     * 用户角色
     *
     * @return
     */
    public Result<List<SysAdminRole>> allRole() {
        Result<List<SysAdminRole>> r = new Result<>(true);
        r.setData(this.getSysAdminRoleMapper().selectByExample(null));
        return r;
    }

    /**
     * 用户角色
     *
     * @return
     */
    public Result<SysAdminRole> byId(Long id) {
        Result<SysAdminRole> r = new Result<>(true);
        r.setData(this.getSysAdminRoleMapper().selectByPrimaryKey(id));
        return r;
    }

    /**
     * 用户角色
     *
     * @return
     */
    public Result<String> save(SysAdminRole sar) {
        Result<String> r = new Result<>(false);
        if (StringUtils.isBlank(sar.getName())) {
            r.setMessage("角色名称不可为空");
            return r;
        }
        // 添加
        Long id = sar.getId();
        if (null == id) {
            this.getSysAdminRoleMapper().insert(sar);
            r.setSuccess(true);
            return r;
        }
        // 修改
        SysAdminRole sarBean = this.getSysAdminRoleMapper().selectByPrimaryKey(id);
        if (null == sarBean) {
            r.setMessage("不存在该条记录");
            return r;
        }
        this.getSysAdminRoleMapper().updateByPrimaryKeySelective(sar);
        r.setSuccess(true);
        return r;
    }


    /**
     * 删除角色
     *
     * @param id 角色主键
     * @return
     */
    public Result<String> remove(Long id) {
        Result<String> r = new Result<>(false);
        if (null == id) {
            r.setMessage("不存在该条记录");
            return r;
        }
        this.getSysAdminRoleMapper().deleteByPrimaryKey(id);
        r.setSuccess(true);
        return r;
    }

    /**
     * 获取用户角色，目前支持单角色
     *
     * @param userId
     * @return
     */
    public SysAdminRole getUserRole(Long userId) {
        SysAdminRoleUserExample sarue = new SysAdminRoleUserExample();
        sarue.createCriteria().andUserIdEqualTo(userId);
        List<SysAdminRoleUser> list = this.getSysAdminRoleUserMapper().selectByExample(sarue);
        SysAdminRoleUser saru = list.size() > 0 ? list.get(0) : null;
        if (null != saru) {
            SysAdminRole sar = this.getSysAdminRoleMapper().selectByPrimaryKey(saru.getRoleId());
            if (null != sar) {
                return sar;
            }
        }
        return null;
    }

    /**
     * 根据角色ID集合获取角色
     *
     * @param roleIds
     * @return
     */
    public List<SysAdminRole> getRolesByIds(String roleIds) {
        String[] idStrs = null;
        List<Long> ids = new ArrayList<>();
        if (StringUtils.isNotBlank(roleIds)) {
            idStrs = roleIds.split(",");
            for (String s : idStrs) {
                ids.add(NumberUtils.toLong(s));
            }
        }
        SysAdminRoleExample sare = new SysAdminRoleExample();
        if (CollectionUtils.isNotEmpty(ids)) {
            sare.createCriteria().andIdIn(ids);
        }
        return this.getSysAdminRoleMapper().selectByExample(sare);
    }

    /**
     * 根据菜单id获取角色列表
     *
     * @param menuId
     * @return
     */
    public List<SysAdminRole> getRolesByMenuId(Long menuId) {
        SysAdminMenuRoleExample samre = new SysAdminMenuRoleExample();
        samre.createCriteria().andMenuIdEqualTo(menuId);
        List<SysAdminMenuRole> list = this.getSysAdminMenuRoleMapper().selectByExample(samre);
        StringBuffer roleIds = new StringBuffer("");
        for (SysAdminMenuRole samr : list) {
            roleIds.append(samr.getRoleId() + ",");
        }
        String roleIdsStr = roleIds.toString();
        if (roleIdsStr.length() > 0) {
            roleIdsStr = roleIdsStr.substring(0, roleIdsStr.length() - 1);
        }
        return this.getRolesByIds(roleIdsStr);
    }

    private SysAdminRoleMapper getSysAdminRoleMapper() {
        return this.sysAdminRoleMapper;
    }

    private SysAdminRoleUserMapper getSysAdminRoleUserMapper() {
        return this.sysAdminRoleUserMapper;
    }

    private SysAdminMenuRoleMapper getSysAdminMenuRoleMapper() {
        return this.sysAdminMenuRoleMapper;
    }
}
